package xijia.plus.docs.manage.all.service.impl;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.config.error.ErrorException;
import io.github.wslxm.springbootplus2.core.enums.Base;
import io.github.wslxm.springbootplus2.core.result.ResultType;
import io.github.wslxm.springbootplus2.core.utils.BeanDtoVoUtil;
import io.github.wslxm.springbootplus2.manage.sys.model.query.ConfigQuery;
import io.github.wslxm.springbootplus2.starter.redis.lock.XjDistributedLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xijia.plus.docs.manage.all.mapper.DocConfigMapper;
import xijia.plus.docs.manage.all.model.dto.DocConfigDTO;
import xijia.plus.docs.manage.all.model.entity.DocConfig;
import xijia.plus.docs.manage.all.model.vo.DocConfigVO;
import xijia.plus.docs.manage.all.service.DocConfigService;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Set;

/**
 * 系统全局配置 ServiceImpl
 *
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>
 * @author ws
 * @email 1720696548@qq.com
 * @date 2023-04-23 11:02:52
 */
@Service
//@DS("#header.tenantName")
public class DocConfigServiceImpl extends BaseServiceImpl<DocConfigMapper, DocConfig> implements DocConfigService {


    @Autowired
    private DataSource dataSource;

    @Override
    @DS("#tenantName")
    public BasePage<DocConfigVO> findPage(String tenantName, ConfigQuery query) {
        LambdaQueryWrapper<DocConfig> queryWrapper = new LambdaQueryWrapper<DocConfig>().orderByAsc(DocConfig::getSort).orderByDesc(DocConfig::getCreateTime).eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(query.getCode()), DocConfig::getCode, query.getCode()).eq(query.getType() != null, DocConfig::getType, query.getType()).likeRight(StringUtils.isNotBlank(query.getName()), DocConfig::getName, query.getName());
        BasePage<DocConfigVO> resPage = null;
        resPage = BeanDtoVoUtil.pageVo(this.page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper), DocConfigVO.class);
        // 如果是富文本不返回内容
        for (DocConfigVO xjAdminConfigVO : resPage.getRecords()) {
            if (xjAdminConfigVO.getType().equals(Base.ConfigType.V3.getValue())) {
                xjAdminConfigVO.setContent("富文本内容请点击详情查看");
            }
        }
        return resPage;
    }

    @Override
    @DS("#tenantName")
    public DocConfigVO findId(String tenantName, String id) {
        return BeanDtoVoUtil.convert(this.getById(id), DocConfigVO.class);
    }

    @Override
    @DS("#tenantName")
    @XjDistributedLock(lockName = "'xj-doc-config_'+#dto.code", waitTime = 5L)
    public String insert(String tenantName, DocConfigDTO dto) {
        // 判code重复
        if (this.count(new LambdaQueryWrapper<DocConfig>().eq(DocConfig::getCode, dto.getCode())) > 0) {
            throw new ErrorException(ResultType.CONFIG_DUPLICATE);
        }
        DocConfig entity = dto.convert(DocConfig.class);
        boolean b = this.save(entity);
        return entity.getId();
    }


    @Override
    @DS("#tenantName")
    @XjDistributedLock(lockName = "'xj-doc-config_'+#dto.code", waitTime = 5L)
    public boolean upd(String tenantName, String id, DocConfigDTO dto) {
        DocConfig config = this.getById(id);
        if (!config.getCode().equals(dto.getCode())) {
            if (this.count(new LambdaQueryWrapper<DocConfig>().eq(DocConfig::getCode, dto.getCode())) > 0) {
                throw new ErrorException(ResultType.CONFIG_DUPLICATE);
            }
        }
        DocConfig entity = dto.convert(DocConfig.class);
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    @DS("#tenantName")
    public boolean del(String tenantName, String id) {
        return this.removeById(id);
    }

    @Override
    @DS("#tenantName")
    public DocConfigVO findByCode(String tenantName, String code) {
        DocConfig xjAdminConfig = this.getOne(new LambdaQueryWrapper<DocConfig>().eq(DocConfig::getCode, code));
        return BeanDtoVoUtil.convert(xjAdminConfig, DocConfigVO.class);
    }

    @Override
    public Set<String> getTenantNames() {
        DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
        Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
        return dataSources.keySet();
    }
}
